/**
 *
 */
package net.haroeris.wh40kcampaign.dao.impl;

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import net.haroeris.wh40kcampaign.beans.po.Campaign;
import net.haroeris.wh40kcampaign.dao.CampaignDao;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author user
 *
 */
@Repository("campaignDao")
@Transactional
public class CampaignDaoImpl implements CampaignDao {

	@PersistenceContext
	private EntityManager entityManager;


	/* (non-Javadoc)
	 * @see net.haroeris.wh40kcampaign.dao.impl.CampaignDao#getCampaign(long)
	 */
	public Campaign getCampaign( long id ){
		return entityManager.find(Campaign.class, id);
	}

	/* (non-Javadoc)
	 * @see net.haroeris.wh40kcampaign.dao.impl.CampaignDao#getAllCampaigns()
	 */
	public Collection<Campaign> getAllCampaigns(){
		TypedQuery<Campaign> thisQuery= entityManager.createNamedQuery( Campaign.QUERY_FINDALL, Campaign.class);
		return thisQuery.getResultList();
	}

	/* (non-Javadoc)
	 * @see net.haroeris.wh40kcampaign.dao.CampaignDao#saveCampaign(net.haroeris.wh40kcampaign.beans.Campaign)
	 */
	public boolean saveCampaign(Campaign campaign) {
		entityManager.merge(campaign);
		return false;
	}

	/* (non-Javadoc)
	 * @see net.haroeris.wh40kcampaign.dao.CampaignDao#createCampaign(net.haroeris.wh40kcampaign.beans.po.Campaign)
	 */
	public boolean createCampaign(Campaign campaign) {
		entityManager.persist(campaign);
		return false;
	}

	/* (non-Javadoc)
	 * @see net.haroeris.wh40kcampaign.dao.CampaignDao#delete(java.lang.Long)
	 */
	public boolean delete(Long id) {
		entityManager.remove(id);
		return true;
	}

	public boolean isExistingEntity(Campaign campaign) {
		return entityManager.contains(campaign);
	}

}